<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset">
      <template #statusSlot="{ model, field }">
        <n-input v-model:value="model[field]" />
      </template>
    </BasicForm>

    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      ref="actionRef"
      :actionColumn="actionColumn"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
    >
      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>

    <n-modal
      v-model:show="showModal"
      :show-icon="false"
      preset="dialog"
      :title="editTitle"
      style="width: 1000px"
    >
      <OrderDetail :order="checkedOrder" />
      <OrderItem :orderId="checkedOrder.id" />
      <OrderShipment
        v-if="checkedOrder.orderStatus == OrderStatusEnum.PAID_OFF"
        :orderId="checkedOrder.id"
        :order="checkedOrder"
        @update:shipment="handleShipment"
      />
      <template #action>
        <n-space>
          <n-button @click="() => (showModal = false)">关闭</n-button>
        </n-space>
      </template>
    </n-modal>
  </n-card>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { useDialog, useMessage } from 'naive-ui';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getOrderPage } from '@/api/order/order';
  import { columns } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import { OrderStatusEnum } from './lib/enum/orderEnum';
  import OrderDetail from './lib/OrderDetail.vue';
  import OrderItem from './lib/OrderItem.vue';
  import OrderShipment from './lib/OrderShipment.vue';
  import { Order } from './lib/entity/Order';
  const schemas: FormSchema[] = [
    {
      field: 'orderType',
      component: 'NInput',
      label: '订单类型',
      rules: [{ required: false }],
    },
    {
      field: 'orderStatus',
      component: 'NInput',
      label: '订单状态',
      rules: [{ required: false }],
    },
    {
      field: 'orderSn',
      component: 'NInput',
      label: '订单编号',
      rules: [{ required: false }],
    },
    {
      field: 'memberMoblie',
      component: 'NInput',
      label: '会员手机号',
      rules: [{ required: false }],
    },
    {
      field: 'buyTime',
      component: 'NDatePicker',
      label: '下单时间',
      componentProps: {
        type: 'datetimerange',
        defaultTime: ['00:00:00', '23:59:59'],
      },
      rules: [{ required: false }],
    },
  ];
  const message = useMessage();
  const actionRef = ref();
  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const checkedOrder = ref(); //当前操作订单
  const editTitle = ref('');
  // const dialog = useDialog();

  const params = ref({});

  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      let action: Array<{}> = [
        {
          label: '订单详情',
          onClick: handleShowDetail.bind(null, record),
        },
      ];
      return h(TableAction as any, {
        style: 'button',
        actions: action,
      });
    },
  });

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:2 xl:5 2xl:4' },
    labelWidth: 100,
    schemas,
  });

  const loadDataTable = async (res) => {
    return await getOrderPage({ ...params.value, ...res, size: 10 });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleShowDetail(record: Recordable) {
    console.log('点击了查看详情', record);
    checkedOrder.value = record;
    editTitle.value = '订单详情';
    showModal.value = true;
  }

  function handleShipment() {
    console.log('发货成功');
    //showModal.value = false;
    checkedOrder.value.orderStatus = OrderStatusEnum.SHIPPED;
    reloadTable();
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    params.value = values;
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
</script>

<style lang="less" scoped></style>
